//
//  CellDetailViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "CellDetailViewController.h"
#import "CellDetailTableViewCell.h"
#import "NewsModel.h"
#import "AfNetWorkingHandler.h"
#import <UIImageView+WebCache.h>
#import "PushWebViewVC.h"
#import "AllDetailModel.h"
#import <MJRefresh.h>
@interface CellDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger number;
@end

@implementation CellDetailViewController
- (void)dealloc
{
    [_str release];
    [_titleStr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = _titleStr;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    //创建tableView
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    [_tableView registerClass:[CellDetailTableViewCell class] forCellReuseIdentifier:@"cellD"];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
     self.number = 10;
    //数据处理
    [self dataHandler];
    
    //    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [_tableView.mj_footer beginRefreshing];
    }
   
}
//上拉加载
- (void)loadnewdata
{
    [AfNetWorkingHandler GetWithStr:_str complection:^(id result) {
        //
        NSArray *arr = result[@"data"][@"articles"];
        NSArray *arrDaiti = [AllDetailModel modelHanderWithArray:arr];
        self.dataArr = [arrDaiti mutableCopy];
        //删除广告 广告data为空
        for (AllDetailModel *m in arrDaiti) {
            if (m.date.length == 0) {
                [_dataArr removeObject:m];
            }
        }
        NSLog(@"%ld", _dataArr.count);
        [_tableView reloadData];
    }];

    [_tableView.mj_footer endRefreshing];
    self.number += 10;
}


//数据处理
-(void)dataHandler
{
//    self.dataArr = [NSMutableArray array];
    [AfNetWorkingHandler GetWithStr:_str complection:^(id result) {
        //
        NSArray *arr = result[@"data"][@"articles"];
        NSArray *arrDaiti = [AllDetailModel modelHanderWithArray:arr];
        self.dataArr = [arrDaiti mutableCopy];
        //删除广告 广告data为空
        for (AllDetailModel *m in arrDaiti) {
            if (m.date.length == 0) {
                [_dataArr removeObject:m];
            }
        }
        NSLog(@"%ld", _dataArr.count);
        [_tableView reloadData];
    }];
    [_tableView.mj_header endRefreshing];
}
-(void)backAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllDetailModel *m = _dataArr[indexPath.row];
    if (m.media.count == 0) {
        return 70;
    }else{
        return 150;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_number < _dataArr.count) {
         return _number;
    }else{
        return _dataArr.count;
    }
//     return 10;
   
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    AllDetailModel *m = _dataArr[indexPath.row];
    if (m.media.count == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"nomalCell"];
        cell.textLabel.text = m.title;
        cell.detailTextLabel.text = [[m.auther_name stringByAppendingString:@"  "] stringByAppendingString:m.date];
        cell.detailTextLabel.textColor = [UIColor grayColor];
        return cell;
    }else{
        CellDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellD"];
        cell.writerLable.text = m.auther_name;
        cell.contentLable.text = m.title;
        cell.timeLable.text = m.date;
        NSString *img = [m.media.firstObject objectForKey:@"url"];
        [cell.picture sd_setImageWithURL:[NSURL URLWithString:img]];
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     AllDetailModel *m = _dataArr[indexPath.row];
     PushWebViewVC *detailVC = [[PushWebViewVC alloc]init];
//    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:detailVC];
    detailVC.pathstr = m.weburl;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
